#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
"""
@File    :   ddns.py
@Time    :   2024-04-22 22:08
@Author  :   坐公交也用券
@Version :   1.0
@Contact :   faith01238@hotmail.com
@Homepage : https://liumou.site
@Desc    :   当前文件作用
"""
import logging
import os
import platform
import sys

# 创建一个自定义的日志记录器
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# 创建一个自定义的日志处理器，设置其输出格式
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(filename)s:%(lineno)d | %(funcName)s | %(message)s')
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(formatter)
logger.addHandler(handler)

arch = platform.machine()
if str(arch).lower() == "x86_64".lower():
	arch = "amd64"
if str(arch).lower() == "aarch64".lower():
	arch = "arm64"
if str(arch).lower() == "aarch".lower() or str(arch).lower() == "aarch32".lower():
	arch = "arm32"
if str(arch).lower() == 'armv7l'.lower():
	arch = "arm32"
arch_list = ["arm64", "amd64", "arm32"]
if arch not in arch_list:
	logger.error("暂不支持当前系统该架构")
	exit(1)

url = f'https://down.liumou.site/client/com.liumou.ddns-tencent/com.liumou.ddns-tencent_linux-{arch}_latest.deb'


class Install:
	def __init__(self):
		self.url = url
		self.file = str(self.url).split('/')[-1]
		self.key = ""
		self.id = ""
		self.domain = ""
		self.subdomain = ""
		self.subdomain4 = ""
		self.subdomain6 = ""
		self.ip_type = "AAAA"
		self.remark = "模板主机"

	def download(self):
		"""
		下载安装包
		:return:
		"""
		if os.system("which wget") != 0:
			os.system("apt-get update")
			os.system("apt-get install -y wget")
			if os.system("which wget") != 0:
				logger.error("未找到wget，请先安装")
				exit(1)
		os.system(f"wget {self.url} -O /tmp/com.liumou.ddns-tencent_linux-{arch}_latest.deb")

	def deb(self):
		"""

		:return:
		"""
		if not os.path.isfile(self.file):
			logger.error("未找到安装包")
			exit(1)
		if os.system(f"dpkg -i {self.file}") != 0:
			logger.error("安装失败")
			exit(1)
	def set(self):
		"""

		:return:
		"""
		txt = f"""export TXY_ID={self.id}
export TXY_KEY={self.key}
export TXY_DOMAIN={self.domain}
export TXY_SUBDOMAIN={self.subdomain}
export TXY_SUBDOMAIN4={self.subdomain4}
export TXY_SUBDOMAIN6={self.subdomain6}
export TXY_IP_TYPE=AAAA
export TXY_REMARK='模板主机'"""
		return txt
	def start(self):
		self.download()
		self.deb()


if __name__ == '__main__':
	install = Install()
	install.start()
